<script type="text/javascript">
function setStatus(txt){parent.statusUpload(txt);}
function setFinish(url){parent.finishUpload(url);}
</script>
<?php
	$curr_dir	= getcwd();
	$dir_path 	= substr($curr_dir, 0, strpos( $curr_dir, "nama" ) )."nama/";
	$sErrUp="";
	if(isset($_POST["UploadFile"]))
	{
		$UploadDir			= base64_decode($_GET['dir']);
		$ReUrl				= base64_decode($_GET['url']);
		if(isset($_FILES['FileLink']['error']))
		{
			foreach ($_FILES["FileLink"]["error"] as $key => $error)
			{ 
				if ($error == UPLOAD_ERR_OK) 
				{
					$tmp_name 	= $_FILES["FileLink"]["tmp_name"][$key]; 
					$filesize	= @getimagesize($tmp_name);
					$name 		= $_FILES["FileLink"]["name"][$key];
					$extension 	= getExtension($_FILES["FileLink"]["name"][$key]);
					//$md5_name	= md5($name).'.'.$extension;
					//$md5_name	= $name).'.'.$extension;
					move_uploaded_file($tmp_name, $UploadDir. '/' .$name);
					//create_thumb($UploadDir."/".$md5_name, $UploadDir."/thumbs/".$md5_name, 150);
					//echo "1";
					//move_uploaded_file($tmp_name, $UploadDir. "/" .$md5_name);
					//$sErrUp="Upload thành công. Nhấn F5 để làm tươi";
				} 
				else
				{
					switch ($error) 
					{ 
					  case 1:  $sErrUp='File quá dung lượng cho phép.';  break; 
					  case 2:  $sErrUp='File quá dung lượng cho phép';  break; 
					  case 3:  $sErrUp='File được up lên không hoàn chỉnh';  break; 
					} 
	
				}
			} 
		}
		if($sErrUp!="")
		{
			echo '<script type="text/javascript">setStatus(\''.$sErrUp.'\');</script>';
		}
		else
		{
			echo '<script type="text/javascript">setFinish(\''.$ReUrl.'\');</script>';
		}
	}
		
	function create_thumb($pathToImage, $pathToThumb, $thumbWidth)
	{
		$dir 		= opendir($pathToImage);
		$fname 		= readdir($dir);
		$info 		= pathinfo($pathToImage . $fname);
		if (strtolower($info['extension'])	== 'jpg')
		{
			$img 		= imagecreatefromjpeg($pathToImage);
		}
		else if (strtolower($info['extension'])	== 'jpeg')
		{
			$img 		= imagecreatefromjpeg($pathToImage);
		}
		else if (strtolower($info['extension'])	== 'gif')
		{
			$img 		= imagecreatefromgif($pathToImage);
		}
		else if (strtolower($info['extension'])	== 'png')
		{
			$img 		= imagecreatefrompng($pathToImage);
		}
		$width 		= imagesx($img);
		$height 	= imagesy($img);
		$new_width 	= $thumbWidth;
		$new_height = floor($height * ($thumbWidth / $width));
		$tmp_img 	= imagecreatetruecolor($new_width, $new_height);
		imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
		if (strtolower($info['extension'])	== 'jpg')
		{
			imagejpeg($tmp_img, $pathToThumb);
		}
		if (strtolower($info['extension'])	== 'jpeg')
		{
			imagejpeg($tmp_img, $pathToThumb);
		}
		else if (strtolower($info['extension']) == 'gif')
		{
			imagegif($tmp_img, $pathToThumb);
		}
		else if (strtolower($info['extension'])	== 'png')
		{
			imagepng($tmp_img, $pathToThumb);
		}
		imagedestroy($tmp_img);
		imagedestroy($img);
		closedir($dir);
	}

	
	function getExtension($str) 
	{
		$i = strrpos($str,".");
		if (!$i) { return ""; }
		$l = strlen($str) - $i;
		$ext = substr($str,$i+1,$l);
		return $ext;
	}


	function utf8_to_ascii($str) 
	{
		$chars = array(
			'a'	=>	array('ấ','ầ','ẩ','ẫ','ậ','Ấ','Ầ','Ẩ','Ẫ','Ậ','ắ','ằ','ẳ','ẵ','ặ','Ắ','Ằ','Ẳ','Ẵ','Ặ','á','à','ả','ã','ạ','â','ă','Á','À','Ả','Ã','Ạ','Â','Ă', 'A'),
			'b'	=> array('B'),
			'c'	=> array('C'),
			'd'	=>	array('đ','Đ','D'),
			'e' =>	array('ế','ề','ể','ễ','ệ','Ế','Ề','Ể','Ễ','Ệ','é','è','ẻ','ẽ','ẹ','ê','É','È','Ẻ','Ẽ','Ẹ','Ê','E'),
			'f'	=> array('F'),
			'g'	=> array('G'),
			'h'	=> array('H'),
			'i'	=>	array('í','ì','ỉ','ĩ','ị','Í','Ì','Ỉ','Ĩ','Ị','I'),
			'j'	=> array('J'),
			'k'	=> array('K'),
			'l'	=> array('L'),
			'm'	=> array('M'),
			'n'	=> array('N'),
			'o'	=>	array('ố','ồ','ổ','ỗ','ộ','Ố','Ồ','Ổ','Ô','Ộ','ớ','ờ','ở','ỡ','ợ','Ớ','Ờ','Ở','Ỡ','Ợ','ó','ò','ỏ','õ','ọ','ô','ơ','Ó','Ò','Ỏ','Õ','Ọ','Ô','Ơ','O'),
			'p'	=> array('P'),
			'q'	=> array('Q'),
			'r'	=> array('R'),
			's'	=> array('S'),
			't'	=> array('T'),
			'u'	=>	array('ứ','ừ','ử','ữ','ự','Ứ','Ừ','Ử','Ữ','Ự','ú','ù','ủ','ũ','ụ','ư','Ú','Ù','Ủ','Ũ','Ụ','Ư','U'),
			'v'	=> array('V'),
			'x'	=> array('X'),
			'y'	=>	array('ý','ỳ','ỷ','ỹ','ỵ','Ý','Ỳ','Ỷ','Ỹ','Ỵ','Y'),
			'z'	=> array('Z')			
		);
		foreach ($chars as $key => $arr) 
			foreach ($arr as $val)
				$str = str_replace($val,$key,$str);
		return $str;
	}
	function replace($str){
		$str1 = strtolower(utf8_to_ascii($str));
		$str2 = str_replace(array(' ', '~', '`', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '+', '=', '{', '}', '[', ']', '|', ':', ';', '"','<', '>', '?', '/', ',', '//', '“', '”'), '_', utf8_to_ascii($str));
		$str3 = str_replace('__','',$str2);
		$str4 = str_replace('__','',$str3);	
		return $str4;
	}
?>